package com.example.jussi.helloworld;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HelloWorldActivity extends BaseActivity {

    private static final String TAG = "HelloWorldActivity";

    Button button0;
    Button button01;
    Button button02;
    Button button03;
    TextView textView0;

    /**
     * Intent 的推荐写法
     * @param context
     * @param data
     */
    public static void actionStart(Context context, String data){
        Intent intent = new Intent(context, HelloWorldActivity.class);
        intent.putExtra("param", data);
        context.startActivity(intent);
    }

    /**
     * 创建活动
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Log.d("HelloWorldActivity", getClass().getSimpleName());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_world_layout);
        button0 = (Button) findViewById(R.id.button0);
        button01 = (Button) findViewById(R.id.button01);
        button02 = (Button) findViewById(R.id.button02);
        button03 = (Button) findViewById(R.id.button03);
        textView0 = (TextView) findViewById(R.id.textView0);

        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FirstActivity.actionStart(HelloWorldActivity.this, "Hello First Activity. ");
                /*Intent intent = new Intent(HelloWorldActivity.this, FirstActivity.class);
                intent.putExtra("param", "Hello FirstActivity");
                startActivity(intent);*/
            }
        });

        /**
         * 隐式 Intent （要在 AndroidManifest 里手动注册过滤器）
         */
        button01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Intent intent = new Intent(HelloWorldActivity.this, SecondActivity.class);
                Intent intent = new Intent("com.example.jussi.helloworld.ACTION_START");
                intent.addCategory("com.example.jussi.helloworld.MY_CATEGORY");
                startActivityForResult(intent,1);
            }
        });

        button02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HelloWorldActivity.this, NormalActivity.class);
                startActivity(intent);
            }
        });

        button03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HelloWorldActivity.this, DialogActivity.class);
                startActivity(intent);
            }
        });

        Intent intent = getIntent();
        String param = intent.getStringExtra("param");
        if(!"".equals(param) && param != null){
            textView0.setText(param);
        }
    }

    /**
     * 获得活动的返回数据
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    textView0 = (TextView) findViewById(R.id.textView0);
                    String returnDate = data.getStringExtra("data_return");
                    Log.d(TAG, returnDate);
                    if(!"".equals(returnDate) && returnDate != null){
                        textView0.setText(returnDate);
                    }
                }
                break;
            default:
                Log.d(TAG, "It is not returned data. ");
        }
    }

    /**
     * 活动中的 Menu
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_item:
                Toast.makeText(this, "You clicked Add. ", Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}
